package com.zmzncs.lmtc.module.shop.controller;


import com.zmzncs.lmtc.common.annotation.Idempotent;
import com.zmzncs.lmtc.common.pojo.LoginUser;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.*;
import com.zmzncs.lmtc.module.customer.pojo.card.MonthCardBuyForm;
import com.zmzncs.lmtc.module.shop.pojo.shop_info.ShopInfoDTO;
import com.zmzncs.lmtc.module.shop.pojo.shop_info.ShopInfoVO;
import com.zmzncs.lmtc.module.shop.pojo.shop_order.GoodsBuyForm;
import com.zmzncs.lmtc.module.shop.pojo.shop_order.ShopOrderGoodsDTO;
import com.zmzncs.lmtc.module.shop.pojo.shop_order.ShopOrderGoodsVO;
import com.zmzncs.lmtc.module.shop.service.ShopOrderService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户-订单 前端控制器
 * </p>
 *
 * @author 高博文
 * @since 2021-05-20
 */
@Api(tags = "商户订单信息")
@RestController
@RequestMapping("/shop/shop-order")
public class ShopOrderController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    ShopOrderService shopOrderService;

    @ApiOperation(value = "去使用商品，返回二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="订单id", required = true),
    })
    @GetMapping("useGoods/{id}")
    public String useGoods(@PathVariable Long id){
        String writeOffCode  = shopOrderService.useGoods(id);
        String jpgBase64 = null;
        if(StringUtils.isNotBlank(writeOffCode)){
            jpgBase64 = QRCodeUtil.encode(writeOffCode);
        }
        return jpgBase64;
    }



    @ApiOperation(value = "商品退款")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="订单id", required = true),
    })
    @GetMapping("refundGoods/{id}")
    @Idempotent(param = "id")
    public Rest refundGoods(@PathVariable Long id){
        return  shopOrderService.refundGoods(id);
    }


    @ApiOperation(value = "获取我的 商品订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query", name = "status", value = "状态", required = true),
    })
    @GetMapping(value = "/getMyOrderInfoListByStatus")
    public Rest<List<ShopOrderGoodsVO>> getMyOrderInfoListByStatus(@NotBlank Long status){
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        List<ShopOrderGoodsDTO>   shopOrderGoodsDTOS = shopOrderService.getMyOrderInfoListByStatus(loginUser.getCustomerId(),status);
        List<ShopOrderGoodsVO> shopOrderGoodsVOS = JsonUtil.listTransform(shopOrderGoodsDTOS, ShopOrderGoodsVO.class);
        return RestUtil.success(shopOrderGoodsVOS);
    }


    @ApiOperation(value = "购买商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/buyGoods")
    @Idempotent
    public Rest buyGoods(@Valid GoodsBuyForm form) {
        Map<String, Object> map = shopOrderService.buyGoods(form);

        return RestUtil.success(map);
    }

    @ApiOperation(value = "微信支付回调-购买商品", consumes = MediaType.TEXT_XML_VALUE)
    @PostMapping(value = "/wechat/buyGoods/callback", consumes = MediaType.TEXT_XML_VALUE)
    public String buyGoodsCallback(HttpServletRequest request) {
        String callbackStr = WeChatUtil.parseCallback(request);
        if (StringUtils.isBlank(callbackStr)) {
            return WeChatUtil.wxpayCallbackResult(false);
        }

        return shopOrderService.buyGoodsCallback(callbackStr);
    }

    @ApiOperation(value = "核销二维码")
    @GetMapping(value = "/generate-qr-code")
    public void generateQRCode(HttpServletResponse response) {
//        QRCodeUtil.encode("www.baidu.com", null, response);
    }

}

